package com.tianji.aigc.tools;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjUtil;
import com.tianji.aigc.config.ToolResultHolder;
import com.tianji.aigc.constants.Constant;
import com.tianji.aigc.tools.result.CourseInfo;
import com.tianji.api.client.course.CourseClient;
import com.tianji.api.dto.course.CourseBaseInfoDTO;
import lombok.RequiredArgsConstructor;
import org.springframework.ai.chat.model.ToolContext;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.stereotype.Component;

/**
 * @author zgp
 * @version V1.0
 * @date 2025-04-23 10:13
 */
@Component
@RequiredArgsConstructor
public class CourseTools {
    private final CourseClient courseClient;

    /**
     * 根据课程id查询课程信息
     * @param courseId 课程id
     * @param toolContext tool上下文，用于获取传递的参数
     * @return 课程信息
     */
    @Tool(description = Constant.Tools.QUERY_COURSE_BY_ID)
    public CourseInfo getCourseInfoById(@ToolParam(description = Constant.ToolParams.COURSE_ID) Long courseId, ToolContext toolContext) {
        if(ObjUtil.isNull(courseId)){
            return null;
        }
        // 远程调用查询课程信息
        CourseBaseInfoDTO courseBaseInfoDTO = courseClient.baseInfo(courseId, true);
        if(ObjUtil.isNull(courseBaseInfoDTO)){
            return null;
        }
        String requestId = Convert.toStr(toolContext.getContext().get(Constant.REQUEST_ID));
        String field = "courseInfo_" + courseId;
        // 将课程信息传入全局Map容器中，用于大模型响应，追加返回给前端，展示课程卡片数据
        CourseInfo courseInfo = CourseInfo.of(courseBaseInfoDTO);
        ToolResultHolder.put(requestId, field, courseInfo);
        return courseInfo;
    }
}
